/**
 * 渠道商
 * */


(function () {
})();


//选项
function getOption(cateid){
    $.ajax({
        type: apis.qdCardOpton.method,
        url: apis.qdCardOpton.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var cateAry = res.data.cate;
            var h = '', i;
            for (i = 0; i < cateAry.length; i ++) {
                if (cateid == cateAry[i].id) {
                    h += '<option value="' + cateAry[i].id + '" selected>';
                } else {
                    h += '<option value="' + cateAry[i].id + '">';
                }
                h += cateAry[i].title;
                h += '</option>';
            }
            $('#cate').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//列表
function getList(){
    $.ajax({
        type: apis.qdCardIndex.method,
        url: apis.qdCardIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            indexHtml(d);
            $('#pageTotal').html(d.length);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//列表字符串
function indexHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].cate_name + '</td>';
        h += '<td>';
        if (d[i].imgLink == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].imgPrefix + d[i].imgLink + '" height="50"/>';
        }
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="f-14">';
        h += '<a href="javascript:;" onclick="card_edit(\'编辑\',\'card-edit.html\', ' + d[i].id + ');" style="text-decoration: none">';
        h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="card_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '<input type="hidden" id="cate' + d[i].id + '" value="' + d[i].cate_id + '"/>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="imgPrefix' + d[i].id + '" value="' + d[i].imgPrefix + '"/>';
        h += '<input type="hidden" id="imgLink' + d[i].id + '" value="' + d[i].imgLink + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//列表
function getOne(v){
    $.ajax({
        type: apis.qdCardOne.method,
        url: apis.qdCardOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#title')[0].value = d.title;
            var h = '', i;
            if (d.imgLink != '') {
                h = '';
                h += '<img src="' + d.imgPrefix + d.imgLink + '"/>';
                h += '<input type="hidden" id="img" value="' + d.imgLink + '"/>';
                $('#bigShow').html(h);
            }
            $('#imgOut').show();
            h = '';
            for (i = 0; i < d.imgList.length; i ++) {
                h += '<img src="' + d.imgList[i] + '"/>';
            }
            $('#bigShow2').html(h);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function card_edit(title,url,id,w,h){
    url += '?id=' + id;
    var cateid = $('#cate' + id).val();
    var tt = $('#title' + id).val();
    var imgPrefix = $('#imgPrefix' + id).val();
    var imgLink = $('#imgLink' + id).val();
    cateid = cateid ? cateid : 0;
    tt = tt ? tt : '';
    imgPrefix = imgPrefix ? imgPrefix : '';
    imgLink = imgLink ? imgLink : '';
    url += '&cateid=' + cateid;
    url += '&title=' + escape(tt);
    url += '&imgPrefix=' + escape(imgPrefix);
    url += '&imgLink=' + escape(imgLink);
    layer_show(title,url,w,500);
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.domain + e.img + '" style="width: ' + e.w + 'px; height: ' + e.h + 'px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h).height(e.h);
}


//更新
function modify(){
    var img = $('#img').val();
    var formData = {
        id: id,
        cateid: $('#cate').val(),
        title: $('#title').val(),
        img: img ? img : '',
    };
    if (!formData.cateid || formData.cateid == 0) {
        //layer.msg('SKU规格必须的',{icon: 0,time: 1000});
        msgPop({msg: '分类必须的'});
        return
    }
    if (!formData.title) {
        msgPop({msg: '名称必须的'});
        return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.qdCardModify.method,
        url: apis.qdCardModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function card_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.qdCardDel.method,
            url: apis.qdCardDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //layer.msg('商品已删除!',{icon: 1,time: 1000});
                layerMsgOk('删除成功');
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//商品列表
function goodsList(){
    $.ajax({
        type: apis.qdCardGlist.method,
        url: apis.qdCardGlist.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            goodsHtml(d);
            $('#pageTotal').html(d.length);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//商品字符串
function goodsHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].card_name + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].code + '</td>';
        h += '<td>';
        if (d[i].imgLink == '') {
            h += '-';
        } else {
            h += '<img src="' + d[i].imgPrefix + d[i].imgLink + '" height="50"/>';
        }
        h += '<td>' + d[i].uptime + '</td>';
        //h += '<td class="f-14">';
        //h += '<a href="javascript:;" onclick="card_edit(\'编辑\',\'card-edit.html\', ' + d[i].id + ');" style="text-decoration: none">';
        //h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        //h += ' &nbsp; ';
        //h += '<a href="javascript:;" onclick="card_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        //h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        //h += '</a>';
        //h += '<input type="hidden" id="cate' + d[i].id + '" value="' + d[i].cate_id + '"/>';
        //h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        //h += '<input type="hidden" id="imgPrefix' + d[i].id + '" value="' + d[i].imgPrefix + '"/>';
        //h += '<input type="hidden" id="imgLink' + d[i].id + '" value="' + d[i].imgLink + '"/>';
        //h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//订单列表
function orderList(){
    var formData = {
        start: pages.start, liit: pages.limit,
    };
    $.ajax({
        type: apis.qdCardOrder.method,
        url: apis.qdCardOrder.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            orderHtml(d);
            $('#pageTotal').html(d.length);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//订单字符串
function orderHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].card_name + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//转增列表
function userList(){
    var formData = {
        start: pages.start, liit: pages.limit,
    };
    $.ajax({
        type: apis.qdCardUser.method,
        url: apis.qdCardUser.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            userHtml(d);
            $('#pageTotal').html(d.length);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//商品字符串
function userHtml(d){
    var h = '', i;
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].uname + '</td>';
        h += '<td>' + d[i].card_name + '</td>';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>' + d[i].code + '</td>';
        h += '<td>';
        if (d[i].is_transfer == 1) {
            h += '已转赠';
            h += '<br/>后ID【' + d[i].new_id + '】';
        } else {
            h += '未转赠<br>';
        }
        h += '</td>';
        h += '<td>';
        h += d[i].is_used==0 ? '未使用' : '已使用';
        h += '</td>';
        //h += '<td class="f-14">';
        //h += '<a href="javascript:;" onclick="card_edit(\'编辑\',\'card-edit.html\', ' + d[i].id + ');" style="text-decoration: none">';
        //h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        //h += ' &nbsp; ';
        //h += '<a href="javascript:;" onclick="card_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        //h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        //h += '</a>';
        //h += '<input type="hidden" id="cate' + d[i].id + '" value="' + d[i].cate_id + '"/>';
        //h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        //h += '<input type="hidden" id="imgPrefix' + d[i].id + '" value="' + d[i].imgPrefix + '"/>';
        //h += '<input type="hidden" id="imgLink' + d[i].id + '" value="' + d[i].imgLink + '"/>';
        //h += '</td>';
        h += '</tr>';
    }
    $('tbody').html(h);
}


//抓图
function uploadImg() {
    var fileObj = $('#bigUpload2')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        $.ajax({
            type: apis.qdCardUpload.method,
            url: apis.qdCardUpload.url,
            data: { id: id, img: res.target.result },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 1,time: 1000});
                    return
                }
                var d = res.data;
                msgPop({msg: '上传成功'});
                //setTimeout(function(){
                    //top.location.reload();
                    //location.reload();
                //}, 1000);
                var h = '', i;
                for (i = 0; i < d.length; i ++) {
                    h += '<img src="/' + d[i].link + '"/>';
                }
                $('#bigShow2').html(h);
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}